using System;
using System.Data;
using System.Data.SqlClient;

namespace MessageBoard
{
	/// <summary>
	/// Summary description for Thread.
	/// </summary>
	public class Thread : Connection
	{
		#region Fields
		private int _Id;
		private Forum _Forum;
		private int _Icon;
		private string _Subject;
		private Boolean _Locked;
		private int _TotalPosts;
		private int _TotalViews;
		#endregion

		#region Constructors
		public Thread()
		{
			_Subject = "";
			_TotalPosts=1;
			_TotalViews=1;
			_Locked=false;
		}
		#endregion

		#region Functions
		public static Thread Get(System.Data.DataRow dr)
		{
			Thread thread = new Thread();
			thread.Id = int.Parse(dr["Id"].ToString());
			thread.Forum = Forum.Get(int.Parse(dr["ForumId"].ToString()));
			thread.Icon = int.Parse(dr["Icon"].ToString());
			thread.Subject = dr["Subject"].ToString();
			thread.TotalPosts = int.Parse(dr["TotalPosts"].ToString());
			thread.TotalViews = int.Parse(dr["TotalViews"].ToString());
			thread.Locked = Convert.ToBoolean(dr["Locked"].ToString());
			return thread;
		}

		public static Thread Get(int id)
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM " + Connection.tblThread + " WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Id", id);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		public static Thread GetLatest(Forum forum)
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT top 1 B.* FROM [" + Connection.tblPost + "] AS A INNER JOIN [" + Connection.tblThread + "] AS B ON A.[ThreadId] = B.[Id] WHERE B.[ForumId] = @ForumId ORDER BY A.[PostDate] DESC", DbConnection);
			getData.SelectCommand.Parameters.Add("@ForumId", forum.Id);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		public static System.Collections.ArrayList GetAll(Forum forum)
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"SELECT *, ( SELECT MAX([PostDate]) FROM [" + Connection.tblPost + "] WHERE [threadId] = [MB_Thread].[id]) AS PostDate FROM [" + Connection.tblThread + "] WHERE [ForumId] = @ForumId ORDER BY [PostDate] DESC", DbConnection);
			getData.SelectCommand.Parameters.Add("@ForumId", forum.Id);
			getData.Fill(ds);
			foreach(System.Data.DataRow dr in ds.Tables[0].Rows)
			{
				arrayList.Add(Get(dr));
			}
			return arrayList;
		}

		public static int Count(Forum forum)
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT count(*) FROM [" + Connection.tblThread + "] WHERE [ForumId] = @ForumId", DbConnection);
			getData.SelectCommand.Parameters.Add("@ForumId", forum.Id);
			getData.Fill(ds);
			return int.Parse(ds.Tables[0].Rows[0][0].ToString());
		}
		#endregion
		
		#region Methods
		public void Insert()
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			INSERT INTO [" + Connection.tblThread + @"] 
			([ForumId], [Icon], [Subject], [TotalPosts], [TotalViews], [Locked])
			VALUES
			(@ForumId, @Icon, @Subject, @TotalPosts, @TotalViews, @Locked);
			SELECT @@identity as i", DbConnection);
			getData.SelectCommand.Parameters.Add("@ForumId", Forum.Id);
			getData.SelectCommand.Parameters.Add("@Icon", Icon);
			getData.SelectCommand.Parameters.Add("@Subject", Subject);
			getData.SelectCommand.Parameters.Add("@TotalPosts", TotalPosts);
			getData.SelectCommand.Parameters.Add("@TotalViews", TotalViews);
			getData.SelectCommand.Parameters.Add("@Locked", Locked);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				Id = int.Parse(ds.Tables[0].Rows[0]["i"].ToString());
			}
		}

		public void Update()
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			UPDATE [" + Connection.tblThread + @"] SET 
			[ForumId] = @ForumId,
			[Icon] = @Icon,
			[Subject] = @Subject,
			[TotalPosts] = @TotalPosts,
			[TotalViews] = @TotalViews,
			[Locked] = @Locked
			WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@ForumId", Forum.Id);
			getData.SelectCommand.Parameters.Add("@Icon", Icon);
			getData.SelectCommand.Parameters.Add("@Subject", Subject);
			getData.SelectCommand.Parameters.Add("@TotalPosts", TotalPosts);
			getData.SelectCommand.Parameters.Add("@TotalViews", TotalViews);
			getData.SelectCommand.Parameters.Add("@Locked", Locked);
			getData.SelectCommand.Parameters.Add("@Id", Id);
			getData.Fill(ds);
		}

		public void Lock()
		{
			Locked = true;
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			UPDATE [" + Connection.tblThread + @"] SET 
			[Locked] = @Locked
			WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Locked", Locked);
			getData.SelectCommand.Parameters.Add("@Id", Id);
			getData.Fill(ds);
		}

		public void Unlock()
		{
			Locked = false;
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			UPDATE [" + Connection.tblThread + @"] SET 
			[Locked] = @Locked
			WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Locked", Locked);
			getData.SelectCommand.Parameters.Add("@Id", Id);
			getData.Fill(ds);
		}

		public void Delete()
		{
			// Delete the related posts first.
			DeletePosts();

			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			DELETE FROM [" + Connection.tblThread + @"] WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Id", Id);
			getData.Fill(ds);
		}

		/// <summary>
		/// Delete the related Posts.
		/// </summary>
		private void DeletePosts()
		{
			if(this.FirstPost!=null)
				this.FirstPost.Delete();
		}
		#endregion

		#region Properties
		/// <summary>
		/// The Id of the thread.
		/// </summary>
		public int Id
		{
			get
			{
				return _Id;
			}
			set
			{
				_Id = value;
			}
		}

		/// <summary>
		/// The forum.
		/// </summary>
		public Forum Forum
		{
			get
			{
				return _Forum;
			}
			set
			{
				_Forum = value;
			}
		}

		/// <summary>
		/// The thread Icon
		/// </summary>
		public int Icon
		{
			get
			{
				return _Icon;
			}
			set
			{
				_Icon = value;
			}
		}

		/// <summary>
		/// The First Post.
		/// </summary>
		public Post FirstPost
		{
			get
			{
				return Post.GetFirst(this);;
			}
		}

		/// <summary>
		/// The Latest Post.
		/// </summary>
		public Post LatestPost
		{
			get
			{
				return Post.GetLatest(this);
			}
		}

		/// <summary>
		/// The Subject of the thread.
		/// </summary>
		public string Subject
		{
			get
			{
				return _Subject;
			}
			set
			{
				_Subject = value;
			}
		}

		/// <summary>
		/// The Total Views to this Thread.
		/// </summary>
		public int TotalViews
		{
			get
			{
				return _TotalViews;
			}
			set
			{
				_TotalViews = value;
			}
		}

		/// <summary>
		/// The Total Posts in this thread.
		/// </summary>
		public int TotalPosts
		{
			get
			{
				return _TotalPosts;
			}
			set
			{
				_TotalPosts = value;
			}
		}

		/// <summary>
		/// If this thread is locked or not.
		/// </summary>
		public Boolean Locked
		{
			get
			{
				return _Locked;
			}
			set
			{
				_Locked = value;
			}
		}

		#endregion
	}
}
